package com.mf.ergate.web.common.utils;

import com.mf.framework.common.model.Page;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 熙康Bean工具类
 * User: zhaoming
 * Date: 2018-08-17
 * To change this template use File | Settings | File Templates.
 **/
public class MfBeanUtils {

    /**
     * 复制对象
     */
    public static <T> T copyObject(Object source, Class<T> targetClass){
        try {
            T t = targetClass.newInstance();
            BeanUtils.copyProperties(source, t);
            return t;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 复制列表对象
     */
    public static <T> List<T> copyList(List sources, Class<T> targetClass) {
        try {
            List<T> list = new ArrayList<>();
            for (Object object : sources) {
                T t = targetClass.newInstance();
                BeanUtils.copyProperties(object, t);
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 复制分页对象
     */
    public static <T> Page<T> copyPage(Page sourcePage, Class<T> targetClass){
        Page<T> page = new Page<>();
        BeanUtils.copyProperties(sourcePage, page);
        page.setContent(copyList(sourcePage.getContent(), targetClass));
        return page;
    }
}
